<?php

class Orders extends Controller 
{
	// Initialises the controller
	function Orders()
	{
		parent::Controller();
		  ////////////////////////////
		//CHECKING FOR PERMISSIONS
		///////////////////////////
		//-------------------------------------------------
        //only 'admin' and 'superadmin' can manage users
        
        $this->freakauth_light->check('admin');
        
        //-------------------------------------------------
        //END CHECKING FOR PERMISSION
        
        $this->_container = $this->config->item('FAL_template_dir').'template_admin/container';
        $this->load->model('orders_model');
        $this->load->library('CNC_orders', 'cnc_orders');
        $this->load->library('email');
        
    
	}
    function index()
    {
		$data['heading']=$this->lang->line('CNC_Admin_User_ViewOrders');
		$data['action']=$this->lang->line('CNC_services');
		$data['content']= $this->get_menu();
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
	    
    }
    function get_menu()
    {
    	$data['content'] = anchor('admin/orders/plumber', 'Plumber orders')." | ";
		$data['content'].= anchor('admin/orders/repairs', 'Repairs')." | ";
		$data['content'].= anchor('admin/orders/carpenter', 'Carpenter')." | ";
		$data['content'].= anchor('admin/orders/electrician', 'Electrician')." | ";
		$data['content'].= anchor('admin/orders/mechanic', 'Mechanic')." | ";
		$data['content'].= anchor('admin/orders/grocery', 'Groceries')." ";
		return $data['content'];
    }
    function plumber()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_plumber');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_orders('plumber');
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        
		$this->load->vars($data);
		$this->load->view($this->_container);
    }
    function repairs()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_repairs');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_orders('repairs');
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
    }
    function carpenter()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_carpenter');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_orders('carpenter');
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
    }
    function electrician()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_electrician');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_orders('electrician');
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
    }
    function mechanic()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_mechanic');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_orders('mechanic');
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
    }
	function grocery()
    {
    	$data['heading']=$this->lang->line('CNC_Admin_services_Heading');
		$data['action']=$this->lang->line('CNC_services_grocery');
		$data['content']= $this->get_menu();
		$data['CNC_content'] = $this->cnc_orders->get_grocery_orders();
		$data['page'] = $this->config->item('FAL_template_dir').'template_admin/example/example';
        $this->load->vars($data);
	    $this->load->view($this->_container);
    }
	function show()
	{
		$id = $this->uri->segment(4);
		$userid = $this->uri->segment(5);
		$order = $this->uri->segment(6);
    	$query = $this->usermodel->getUserById($id);
		if ($query->num_rows() == 1)
        {
            $row = $query->row();
            
            // initializing two flags, for the edit and delete links
            // we can edit the displayed user if
            //  - we are an admin
            //  - OR the displayed user is not a superadmin
            $data['can_edit_user'] =
                ($row->role != 'superadmin' AND $row->role != 'admin');
            // we cannot delete a superadmin or an admin
            $data['can_delete_user'] =
                ($row->role != 'superadmin' AND $row->role != 'admin');
                
			$data['user']['id']= $row->id;
			$data['user']['user_name']= $row->user_name;
			$data['user']['email']= $row->email;
			$data['user']['role']= $row->role;
			$data['user']['banned']= $row->banned;
			
			//$countries = null;            
		    if ($this->config->item('FAL_use_country') && strlen($row->country_id))
		    {
		    	$query = $this->country_model->getCountryById($row->country_id);
                if (isset($query))
                {
                    $row = $query->row();
                        
                    //SELECT name FROM country WHERE id= $data['user']['country_id']
                    $data['user']['country'] = $row->name;
                }
		    }
		    
		    if (isset($query)) $query->free_result();
		    
		    if ($this->config->item('FAL_create_user_profile')==TRUE)
		    {
		    	
		    	$data['user_profile']= $this->freakauth_light->_getUserProfile($id);
		    	$data['f_r'] = $this->freakauth_light->_buildUserProfileFieldsRules();
		    	$data['label'] = $data['f_r']['fields']; 
        		if($userid)
        		{
        			$data['service'] = $this->orders_model->ordered_service($userid);
        		}
		    }
        }
        else 
        {
        	$data['error_message']=$this->lang->line('CNC_Admin_ErrorMessage');
        }
    		$status = $this->input->post('status');
    		$mail = $this->input->post('hid');
    		if($status)
    		{
    			$subject = $this->config->item('FAL_website_name').' status of your '. $order.' request ';
    			$message = 'welcome user, your status for '.$order.' is '.$status;
    			if($this->orders_model->order_status($status,$userid))
    			{
    				$this->db_session->set_flashdata('flashMessage', 'Status changed successfully');
    				$this->email($mail,$subject,$message);
    				redirect('admin/orders/show/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6));
    				
    			}
    		}
	    	$data['heading'] = $this->lang->line('CNC_Admin_User_ViewOrders');
	    	$data['action'] = $this->lang->line('CNC_Admin_User_ViewOrders');
	    	$data['controller'] = 'orders';
	    	$data['page'] = $this->config->item('FAL_template_dir').'template_admin/users/detail';

	        $this->load->vars($data);
	        
	    	$this->load->view($this->_container);
	    	
	    	// for debugging
	    	// $this->output->enable_profiler(TRUE);
		
    	
	}
	function email($mail,$subject,$message)
	{
		$this->email->clear();
		$this->email->from($this->config->item('FAL_user_support'), $this->config->item('FAL_website_name'));
    	$this->email->to($mail);
    	$this->email->subject($subject);
		$this->email->message($message);
		$this->email->send();
	}
}
?>